﻿module Sustainalytics.Authentication {

    export class UserInfoService implements IUserInfoService {

        private userInfo: Common.IUserInfo;

        constructor() {
        }

        public setUserInfo(userInfo: Common.IUserInfo): void {
            localStorage.setItem('sust_userInfo', JSON.stringify(userInfo));
            this.userInfo = userInfo;
        }

        public getUserInfo(): Common.IUserInfo {
            if (!this.userInfo) {
                this.userInfo = <Common.IUserInfo>JSON.parse(localStorage.getItem('sust_userInfo'));
            }

            if (!this.userInfo) {
                this.userInfo = this.createEmptyUserInfo();
            }

            return this.userInfo;
        }

        private createEmptyUserInfo() {

            return {
                username: null,
                email: null,
                name: null,
                isAuthenticated: false,
                userId: null, account: "",
                accountId: null,
                accountTier: 0,
                esgProfileId: null,               
            };
        }
    }


    export var userInfoServiceName = 'UserInfoService';

    angular.module(authenticationModuleName).service(userInfoServiceName, UserInfoService);
} 